/*
 * MIT License
 * 
 * Copyright (c) 2024-2025 milkpotatoes
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <securec.h>
#include <unicode/unistr.h>
#include <vector>

#include "include/js_textencoder.h"

napi_value JsTextEncoder::Init(napi_env env, napi_value global)
{
    std::vector<napi_property_descriptor> desc {
        {"_innerEncode", nullptr, Encode,  nullptr, nullptr, nullptr, napi_default_jsproperty, nullptr},
        {"_innerEncodeInto", nullptr, EncodeInto,  nullptr, nullptr, nullptr, napi_default_jsproperty, nullptr},
    };
    napi_value decoderUtils = utils::CreateObject(env);
    napi_define_properties(env, decoderUtils, desc.size(), desc.data());
    
    napi_set_named_property(env, global, "_innerEncoderUtils", decoderUtils);
    return decoderUtils;
}

DEF_NAPI_CALLBACK(JsTextEncoder::Encode, info)
{
    napi_env env = info.GetEnv();
    napi_value str = info[1];
    void* data = nullptr;
    size_t len = 0;
    napi_value buffer = nullptr;
    napi_get_value_string_utf8(env, str, nullptr, 0, &len);
    if (napi_create_arraybuffer(env, len, &data, &buffer) != napi_ok) return nullptr;
    auto copy = new char[len + 1];
    if (copy == nullptr) return nullptr;
    napi_get_value_string_utf8(env, str, copy, len + 1, &len);
    errno_t err = memcpy_s(data, len, copy, len);
    delete [] copy;
    if (err != 0) return nullptr;
    return buffer;
}

DEF_NAPI_CALLBACK(JsTextEncoder::EncodeInto, info)
{
    napi_env env = info.GetEnv();
    napi_value str = info[1];
    napi_value buffer = info[2];  // 2: index of arg buffer
    void* data = nullptr;
    size_t len = 0;
    if (napi_get_arraybuffer_info(env, buffer, &data, &len) != napi_ok) return nullptr;
    auto copy = new char[len + 1];
    if (copy == nullptr) return nullptr;
    napi_get_value_string_utf8(env, str, copy, len + 1, &len);
    errno_t err = memcpy_s(data, len, copy, len);
    delete [] copy;
    if (err != 0) return nullptr;
    napi_value result = utils::CreateArray(env, 2); // 2: size of result array
    {
        icu::UnicodeString unicodeString = icu::UnicodeString::fromUTF8(
            icu::StringPiece(static_cast<char*>(data), len));
        napi_set_element(env, result, 0, utils::CreateUint32(env, unicodeString.length()));
        napi_set_element(env, result, 1, utils::CreateUint32(env, len));
    }
    return result;
}
